package sevlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.DBUtil;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();
    //处理两个请求：博客列表页和博客详情页发出的请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BlogDao blogDao = new BlogDao();
        String respJson = null;
        String blogId = req.getParameter("blogId");
        if(blogId == null) {
            //博客列表页发出的请求，服务器接收，并返回响应。
            //1.通过查询数据库拿到所有的博客列表
            List<Blog> lists = blogDao.getBlogs();
            //2.构造Json字符串返回浏览器
            respJson = objectMapper.writeValueAsString(lists);
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write(respJson);
        }else {
            //博客详情页发出的请求，服务器接收，并返回响应。
            Blog blog = blogDao.getBlog(Integer.parseInt(blogId));
            if(blog == null) {
                resp.setContentType("application/json;charset=utf8");
                resp.getWriter().write("没有查到对应的博客内容");
            }else {
                respJson = objectMapper.writeValueAsString(blog);
                System.out.println("respJson:" + respJson);
                resp.setContentType("application/json;charset=utf8");
                resp.getWriter().write(respJson);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.读取请求中的参数
        req.setCharacterEncoding("utf8");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        //判断是否合法
        if(title == null || content == null || content.length() == 0 || title.length() == 0) {
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write("传过来的正文或者标题为空，无法添加");
            return;
        }
        //2.从会话中拿出当前登录用户的userId
        HttpSession session = req.getSession(false);
        if(session == null) {
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write("当前用户未登录");
            return;
        }
        User user = (User) session.getAttribute("user");
        if(user == null) {
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write("当前用户未登录");
            return;
        }
        //3.构造blog对象，并插入
        BlogDao blogDao = new BlogDao();
        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(user.getUserId());
        blogDao.insert(blog);
        //4.重定向到博客列表页
        resp.sendRedirect("blog_list.html");
    }
}
